package com.microframework.boot.system.service.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.jfinal.plugin.activerecord.Page;
import com.microframework.base.web.page.PageQuery;
import com.microframework.boot.system.model.domain.Role;
import com.microframework.boot.system.service.IRoleService;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class RoleServiceImpl implements IRoleService {

	@Override
	public Page<Role> pages(PageQuery<Role> pageQuery) {
		try {
			StringBuilder sqlBuilder = new StringBuilder("FROM " + Role.TABLE_NAME + " WHERE 1=1");
			List<Object> params = new ArrayList<>();

			Role query = pageQuery.getQuery();
			if (query != null) {
				// 构建查询条件

				if (StringUtils.hasText(query.getCnName())) {
					sqlBuilder.append(" AND cn_name LIKE ?");
					params.add("%" + query.getCnName() + "%");
				}
				
				if (StringUtils.hasText(query.getEnName())) {
					sqlBuilder.append(" AND en_name LIKE ?");
					params.add("%" + query.getEnName() + "%");
				}
				
				if (StringUtils.hasText(query.getCode())) {
					sqlBuilder.append(" AND code LIKE ?");
					params.add("%" + query.getCode() + "%");
				}
				
				if (StringUtils.hasText(query.getEnabled())) {
					sqlBuilder.append(" AND enabled = ? ");
					params.add(query.getEnabled());
				}
				if (StringUtils.hasText(query.getIsDel())) {
					sqlBuilder.append(" AND is_del = ?");
					params.add(query.getIsDel());
				}else {
					sqlBuilder.append(" AND is_del = ?");
					params.add("0");
				}
			}
			else {
				sqlBuilder.append(" AND is_del = ?");
				params.add("0");
			}
			
			// 设置排序
			String orderBy = StringUtils.hasText(pageQuery.getOrderBy()) ? pageQuery.getOrderBy() : " modify_time";
			String orderDirection = StringUtils.hasText(pageQuery.getOrderDirection()) ? pageQuery.getOrderDirection()
					: "desc";
			sqlBuilder.append(" ORDER BY " + orderBy + " " + orderDirection);
			String select="select id,cn_name,en_name,code,enabled,remark,create_time,modify_time ";
			return Role.dao.paginate(pageQuery.getPageNumber(), pageQuery.getPageSize(), select,sqlBuilder.toString(), params.toArray());
		} catch (Exception e) {
			log.error("查询角色列表异常{}", e.getMessage());
			return new Page<Role>(new ArrayList<>(), 1, 10, 0, 0);
		}
	}

	@Override
	public List<Role> options(PageQuery<Role> pageQuery) {
		try {
			StringBuilder sqlBuilder = new StringBuilder("FROM " + Role.TABLE_NAME + " WHERE 1=1");
			List<Object> params = new ArrayList<>();

			Role query = pageQuery.getQuery();
			if (query != null) {
				// 构建查询条件

				if (StringUtils.hasText(query.getCnName())) {
					sqlBuilder.append(" AND cn_name LIKE ?");
					params.add("%" + query.getCnName() + "%");
				}
				
				if (StringUtils.hasText(query.getEnName())) {
					sqlBuilder.append(" AND en_name LIKE ?");
					params.add("%" + query.getEnName() + "%");
				}
				
				if (StringUtils.hasText(query.getCode())) {
					sqlBuilder.append(" AND code LIKE ?");
					params.add("%" + query.getCode() + "%");
				}
				
				if (StringUtils.hasText(query.getEnabled())) {
					sqlBuilder.append(" AND enabled = ? ");
					params.add(query.getEnabled());
				}
				if (StringUtils.hasText(query.getIsDel())) {
					sqlBuilder.append(" AND is_del = ?");
					params.add(query.getIsDel());
				}else {
					sqlBuilder.append(" AND is_del = ?");
					params.add("0");
				}
			}
			else {
				sqlBuilder.append(" AND is_del = ?");
				params.add("0");
			}
			
			// 设置排序
			String orderBy = StringUtils.hasText(pageQuery.getOrderBy()) ? pageQuery.getOrderBy() : " modify_time";
			String orderDirection = StringUtils.hasText(pageQuery.getOrderDirection()) ? pageQuery.getOrderDirection()
					: "desc";
			sqlBuilder.append(" ORDER BY " + orderBy + " " + orderDirection);
			String select="select id,cn_name,en_name,code,enabled,remark ";
			return Role.dao.find(select+sqlBuilder.toString(), params.toArray());
		} catch (Exception e) {
			log.error("查询异常{}", e.getMessage());
			return new LinkedList<Role>();
		}
	}


}
